/*
 * 添加订单主表
 * 参数   交易码：trancode=15112
 * 
 */

package org.ccams.tran;
import java.math.BigDecimal;
import org.ccams.util.Utils;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.ccams.domain.InsertHelp;
import org.ccams.domain.TSalesordersD;//用于保有存明细数据的引用
import org.ccams.domain.User;

import org.ccams.web.UserSession;
import org.ccams.web.WebBeanFactory;
import org.json.JSONArray;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

/**
 * 添加订单主表交易
 * 
 * @author liuguangshuai@gmail.com
 * @系统名称：定制化辅助生成制造系统
 */
public class Tran21112 extends AbstractJsonTran {

	@SuppressWarnings("unchecked")
	public void buildJSONObject(Map model, JSONObject json,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// 获得用户录入信息
		//从页面上获得信息
		String customer_name = (String) request.getParameter("idtclient");		
		System.out.println("测试页面数据有没有传到交易代码里来客户名称:"+customer_name);
		String suser=(String) request.getParameter("suser");
		System.out.println("测试页面数据有没有传到交易代码里来用户姓名:"+suser);		
		String susertel=(String) request.getParameter("susertel");
		String suseraddress=(String) request.getParameter("suseraddress");		
		String customer_id = (String) request.getParameter("idt_client");
		String freight_terms_id= (String) request.getParameter("freight_terms_id");
		String idtOrdertype = (String) request.getParameter("idtOrdertype");
		String idtDept = (String) request.getParameter("idtDept");		
		String salesChannelCode = (String) request.getParameter("salesChannelCode");
		String idtPricelist = (String) request.getParameter("idtPricelist");
		String ordered_date = (String) request.getParameter("ordered_date");
		String username1 = (String) request.getParameter("username1");
		String username2 = (String) request.getParameter("username2");
//		String meaning = (String) request.getParameter("meaning");
		String order_money = (String) request.getParameter("order_money");
		String remark = (String) request.getParameter("remark");
		//获得明细信息
		String params = (String) request.getParameter("salesordersd");
		
		//获得订单创建人
		HttpSession session = request.getSession();
		UserSession userSession = (UserSession)session.getAttribute("userSession");
		User user = userSession.getUser();
		String created_user=user.getUserName();
		
		InsertHelp insertHelp=new InsertHelp();
		//字符串空值处理
		remark = (remark != null) ? remark : "";	
		if(idtPricelist==null||idtPricelist.equals("")){
			insertHelp.setFreight_terms_id(new Integer(0));
		}else{
			insertHelp.setIdtPricelist(new Integer(idtPricelist));
		}
		if(customer_id==null||customer_id.equals("")){
			insertHelp.setId(new Integer(0));
		}else{
			insertHelp.setId(new Integer(customer_id));
		}
		insertHelp.setCustomer_name(customer_name);
		insertHelp.setFreight_terms_id(new Integer(freight_terms_id));
		insertHelp.setOrdertype_id(new Integer(idtOrdertype));
		insertHelp.setIdtDept(Integer.parseInt(idtDept));
//		insertHelp.setMeaning(meaning);
		insertHelp.setOrdered_date(ordered_date);		
		insertHelp.setUsername1(username1);
		insertHelp.setUsername2(username2);
		insertHelp.setRemark(remark);
		insertHelp.setCreated_user(created_user);
		insertHelp.setSalesChannelCode(salesChannelCode);
//		insertHelp.setIdtPricelist(Integer.parseInt(idtPricelist));
		insertHelp.setOrder_money(new BigDecimal(order_money));
		insertHelp.setState(0);
		insertHelp.setSuser(suser);
		insertHelp.setSusertel(susertel);
		insertHelp.setSuseraddress(suseraddress);
			
		JSONArray salesordersd = new JSONArray(params);
		HashMap<String, Object> jsonMap = new HashMap<String, Object>();
		boolean success = false;
		String message = "";	
		int k=0;		
		DataSourceTransactionManager txm = (DataSourceTransactionManager) WebBeanFactory.getBean("transactionManager",request);
		DefaultTransactionDefinition def = new DefaultTransactionDefinition();
		def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
		TransactionStatus status = txm.getTransaction(def);
		try{
			k = getCcamsService().getTsalesordershDao().insertSalesordersh(insertHelp);//添加订单主表信息
			//获得刚才添加的主表的id号线程安全的
			int id = getCcamsService().getTsalesordershDao().getNewId();
			//添加订单从表信息
			System.out.println("明细个数"+salesordersd.length());
			//保存明细数据
			for(int i=0;i<salesordersd.length();i++){
				org.json.JSONObject ordersd = salesordersd.optJSONObject(i);
					TSalesordersD t = new TSalesordersD();
					t.setIdt_salesorders_h(id);
					t.setLine_id(new Long(i+1));
					t.setGoodsid(ordersd.getLong("goodsid"));//目前初始化以后修改
					t.setRequest_date(ordersd.getString("request_date"));
					t.setSchedule_ship_date(ordersd.getString("schedule_ship_date"));
					t.setSchedule_arrival_date(ordersd.getString("schedule_arrival_date"));
					t.setProductdate(new Integer(ordersd.getString("productdate")));
					//获得当期时间
					t.setOrder_date(Utils.getCurDate().toString());
					t.setGoodsname(ordersd.getString("goodsname"));
					t.setSpec(ordersd.getString("spec"));
					t.setSale_price(new BigDecimal(ordersd.getString("sale_price")));
					t.setOrder_quantity_uom(new BigDecimal(ordersd.getString("order_quantity_uom")));
					t.setFlow_status_code(ordersd.getString("flow_status_code"));
					t.setAttrib1(new BigDecimal(ordersd.getString("attrib1")));
					t.setAttrib1_unit(ordersd.getString("attrib1_unit"));
					t.setAttrib2(new BigDecimal(ordersd.getString("attrib2")));
					t.setAttrib2_unit(ordersd.getString("attrib2_unit"));
					t.setAttrib3(new BigDecimal(ordersd.getString("attrib3")));
					t.setAttrib3_unit(ordersd.getString("attrib3_unit"));
					t.setAttrib4(new BigDecimal(ordersd.getString("attrib4")));
					t.setAttrib4_unit(ordersd.getString("attrib4_unit"));
					t.setAttrib5(new BigDecimal(ordersd.getString("attrib5")));
					t.setAttrib5_unit(ordersd.getString("attrib5_unit"));				
					k = getCcamsService().getTsalesordersdDao().insertSalesordersd(t);//执行添加			
			}
		}catch(Exception ex){
			txm.rollback(status);
            ex.printStackTrace();
		}finally{
			txm.commit(status);
		}		
		if (k == 1) {
			success = true;
			message = "订单添加成功";
		} else {
			success = false;
			message = "订单添加失败";
		}
		jsonMap.put("success", success);
		jsonMap.put("message", message);
		json.putAll(jsonMap);
	}
}
